const crypto = require('crypto');

const key = crypto.randomBytes(32); // 生成一个32字节的密钥
const iv = crypto.randomBytes(16); // 生成一个16字节的初始向量
// 加密
export function encrypt (text) {
  const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
  let encrypted = cipher.update(text, 'utf8', 'hex');
  encrypted += cipher.final('hex');
  return encrypted;
}

// 解密
export function decrypt (encrypted) {
  const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
  let decrypted = decipher.update(encrypted, 'hex', 'utf8');
  decrypted += decipher.final('utf8');
  return decrypted;
}


// const text = '需要加密的文本';
// const encrypted = encrypt(text, key, iv);
// console.log('加密后的文本：', encrypted);

// const decrypted = decrypt(encrypted, key, iv);
// console.log('解密后的文本：', decrypted);
